package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.entity.AbilityModel;
import com.a01_2021.thousandstimes.general.entity.Experience;
import com.a01_2021.thousandstimes.general.service.IAbilityModelService;
import com.a01_2021.thousandstimes.instance.entity.Position;
import com.a01_2021.thousandstimes.instance.service.IPositionService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/position")
public class PositionController {

    @Resource
    IPositionService positionService;

    @Resource
    IAbilityModelService abilityModelService;

    /**
     * 用于添加职位
     *
     * @param instanceId 实例id
     * @param position   职位实体
     * @return AjaxResponse
     * @throws CustomException 当存在同名职位、不存在职位标签时抛出该异常
     */
    @PreAuthorize("hasAuthority('position-add')")
    @DS("#header.instanceid")
    @PostMapping("add")
    public AjaxResponse add(@RequestHeader("instanceid") String instanceId, Position position) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        Position resultPosition = positionService.add(position);
        AbilityModel abilityModel = abilityModelService.getFromMaster(position.getPostId());
        abilityModel.setPositionId(position.getId());
        abilityModelService.insert(abilityModel);
        return AjaxResponse.success(resultPosition);
    }

    /**
     * 获取全部职位
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('position-getAll')")
    @DS("#header.instanceid")
    @PostMapping("getAll")
    public AjaxResponse getAll(@RequestHeader("instanceid") String instanceId) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        List<Position> positionList = positionService.getAll();
        return AjaxResponse.success(positionList);
    }

    /**
     * 为用户更换职位
     *
     * @param userId     用户id
     * @param positionId 职位id
     * @return 返还新职位
     */
    @PreAuthorize("hasAnyAuthority('position-changePosition')")
    @DS("#header.instanceid")
    @PostMapping("changePosition")
    public AjaxResponse changePosition(@RequestHeader("instanceid") String instanceId, long userId, long positionId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        Experience experience = positionService.changePosition(userId, positionId, Long.parseLong(instanceId));
        return AjaxResponse.success(experience);
    }
}
